----------------------------------------------------------------------------------
-- Company: 
-- Engineer: 
-- 
-- Create Date:    16:12:42 12/02/2014 
-- Design Name: 
-- Module Name:    branch_decision - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: 
--
-- Dependencies: 
--
-- Revision: 
-- Revision 0.01 - File Created
-- Additional Comments: 
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity branch_decision is
    Port ( newpc_alu : in  STD_LOGIC_VECTOR (15 downto 0);
           cond_data2 : in  STD_LOGIC_VECTOR (15 downto 0);
           instruction : in  STD_LOGIC_VECTOR (15 downto 0);
           branch_en : out  STD_LOGIC;
			  --branch_en_in : in  STD_LOGIC;
           branch_pc : out  STD_LOGIC_VECTOR(15 downto 0));
end branch_decision;

architecture Behavioral of branch_decision is

begin

	process(newpc_alu, cond_data2, instruction)
	begin
		branch_en <= '0';
		--if branch_en_in = '1' then
			case instruction(15 downto 11) is
			when "00010"=>--b
				--branch_pc<=newpc_alu;
				branch_pc<=STD_LOGIC_VECTOR(unsigned(newpc_alu)+1);
				branch_en<='1';
			when "00100"=>--beqz
				if cond_data2 = "0000000000000000" then
					--branch_pc<=newpc_alu;
					branch_pc<=STD_LOGIC_VECTOR(unsigned(newpc_alu)+1);
					branch_en<='1';
				end if;
			when "00101"=>--bneqz
				if cond_data2 /= "0000000000000000" then
					--branch_pc<=newpc_alu;
					branch_pc<=STD_LOGIC_VECTOR(unsigned(newpc_alu)+1);
					branch_en<='1';
				end if;
			when "01100"=>--bteqz
				if instruction(10 downto 8) = "000" then
					if cond_data2 = "0000000000000000" then
						--branch_pc<=newpc_alu;
						branch_pc<=STD_LOGIC_VECTOR(unsigned(newpc_alu)+1);
						branch_en<='1';
					end if;
				end if;
			when "11101"=>--jr
				if instruction(7 downto 0) = "00000000" then
					branch_pc<=cond_data2;
					branch_en<='1';
				end if;
			when others=>
			end case;
		--end if;
	end process;
end Behavioral;

